package com.itheima.edu.day19File.day20ObjectStream;


import java.io.*;
import java.util.ArrayList;

public class StudentAdminOther {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Student stu1 = new Student("zhangsan",23);
        Student stu2 = new Student("lisi",24);
        Student stu3 = new Student("wangwu",25);
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\Develop\\javaCode\\itheima-edu-info-manager\\src\\com\\itheima\\edu\\day19File\\day20ObjectStream\\c.txt"));

        //创建一个集合，存储学生对象，并且直接将集合对象写入文件
        ArrayList<Student> list = new ArrayList<>();
        list.add(stu1);
        list.add(stu2);
        list.add(stu3);
        oos.writeObject(list);
        oos.close();

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\Develop\\javaCode\\itheima-edu-info-manager\\src\\com\\itheima\\edu\\day19File\\day20ObjectStream\\c.txt"));
        //先将集合读入到内存，然后直接遍历读出来的集合
        //注意记得强制转换，将Object类型，转换成所需要的集合类型
        ArrayList<Student> listStu = (ArrayList<Student>) ois.readObject();
        for (Student student : listStu) {
            System.out.println(student);
        }

        ois.close();
    }
}
